using TFlex.Model;
using TFlex.Model.Model3D.Geometry;
using TFlex.Model.Model3D.Visual.Manipulators;

namespace External3D
{
	// Manipulator for dynamic editing of point coordinates
	//
	// Манипулятор для редактирования координат точки
	public class PointManipulator : BaseManipulator
	{
		// Point coordinates
		//
		// Координаты точки
		protected Point3D m_position = new Point3D(0.0, 0.0, 0.0);
		public Point3D Position
		{
			get { return new Point3D(m_position); }
			set { m_position = new Point3D(value); }
		}

		// Manipulator with which the user actually interacts
		//
		// Манипулятор, посредством которого пользователь задаёт положение точки
		protected CoordSystemManipulator m_manipulator = null;

		// Constructs a manipulator
		//
		// Конструктор
		public PointManipulator(Document document, External3DPlugin plugin)
			: base(document, plugin)
		{
		}

		// Activates this manipulator
		//
		// Запустить манипулятор
		public void Start()
		{
			if (m_manipulator != null)
				return;

			m_manipulator = new CoordSystemManipulator();
			m_manipulator.ResetCoordSystem(m_position, m_xAxis, m_yAxis, m_zAxis);
			m_manipulator.ModificationEnqueued += OnUpdate;
			m_manipulator.ModificationCommited += OnCommit;

			BaseActivated(BaseManipulator.IDManipulator.Point);
		}

		// Deactivates the manipulator
		//
		// Остановить манипулятор
		public void Stop()
		{
			if (m_manipulator == null)
				return;

			m_manipulator.Dispose();
			m_manipulator = null;
		}

		// This method is called when the user moves the manipulator
		//
		// Изменение параметров манипулятора
		private void OnUpdate(object sender, ModificationEnqueuedEventArgs args)
		{
			CoordSystemManipulator manipulator = args.Manipulator as CoordSystemManipulator;
			if (manipulator != null)
				m_position = manipulator.GetPendingOrigin();

			BaseUpdated(BaseManipulator.IDManipulator.Point);
		}

		// This method is called when the user finishes moving the manipulator
		//
		// Конец изменения параметров манипулятора
		private void OnCommit(object sender, ModificationCommitedEventArgs args)
		{
			CoordSystemManipulator manipulator = args.Manipulator as CoordSystemManipulator;
			if (manipulator != null)
				m_position = manipulator.GetCommitedOrigin();

			BaseReleased(BaseManipulator.IDManipulator.Point);
		}
	};
}
